﻿using System;
using System.Collections;
using System.Xml;
using MediaBot.Entities;
using MediaBot.Library;
using RakutenCrawler.ConfigClass;
using RakutenCrawler.Repository;

namespace RakutenCrawler.PageProcessors
{
    public class HomePageProcessor : PageProcessor
    {
        public override bool Execute()
        {
            var genreRepo = new GenreRepository();
            var subGenreRepo = new SubGenreRepository();
            var linkRepo = new LinkRepository();

            log.Info("HomePageProcessor.Execute()");
            log.Info("Link: " + Link.Url);

            try
            {
                var serializer = new XmlSerializer<GenreConfig>();
                var genreConfig = serializer.Deserialize(LinkConfig.BuildPath());

                var ns = new NamespaceUriItem { Name = "ns", Value = "http://www.w3.org/1999/xhtml" };
                var namespaces = new ArrayList { ns };

                var xmlWebPage = new XmlWebPage(new Uri(Link.Url), namespaces);
                xmlWebPage.Download();

                var nsManager = xmlWebPage.XmlNamespaceManager;

                var genreNodes = xmlWebPage.SelectNodes(genreConfig.GenreNode.Context);
                foreach (XmlNode genreNode in genreNodes)
                {
                    var genreName = genreConfig.GenreName.GetString(genreNode, nsManager);
                    var genreUrl = genreConfig.GenreUrl.GetString(genreNode, nsManager);

                    var genre = new Genre { GenreName = genreName, GenreURL = genreUrl };
                    genreRepo.InsertOrUpdate(genre);
                    genreRepo.Save();

                    var subGenreNodes = genreNode.SelectNodes(genreConfig.SubGenre.SubGenreNode.Context, nsManager);

                    foreach (XmlNode subGenreNode in subGenreNodes)
                    {
                        var subGenreName = genreConfig.SubGenre.SubGenreName.GetString(subGenreNode, nsManager);
                        var subGenreUrl = genreConfig.SubGenre.SubGenreUrl.GetString(subGenreNode, nsManager);
                        var subGenre = new SubGenre { SubGenreName = subGenreName, SubGenreURL = subGenreUrl };

                        subGenre.GenreId = genre.GenreId;
                        subGenreRepo.InsertOrUpdate(subGenre);
                        subGenreRepo.Save();

                        linkRepo.InsertOrUpdate(new Link { Url = subGenreUrl, SubGenreId = subGenre.SubGenreId });                        
                    }
                    linkRepo.Save();
                }
            }
            catch (Exception e)
            {
                log.Error("RakutenHomePageProcessor.Execute() exception", e);
                return false;
            }
            finally
            {
                genreRepo.Dispose();
                subGenreRepo.Dispose();
                linkRepo.Dispose();
            }

            return true;
        }
    }
}
